package com.google.code.ptrends.excel.entities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Row implements Iterable<Cell> {

	private int index;
	private List<Cell> cells;

	private int fHashCode;

	public Row() {
		cells = new ArrayList<Cell>();
	}

	public Row(int index, Collection<Cell> cells) {
		this.index = index;
		if (cells != null) {
			this.cells = new ArrayList<Cell>(cells);
		}
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public Collection<Cell> getCells() {
		return cells;
	}

	public void setCells(final Collection<Cell> cells) {
		this.cells = new ArrayList<Cell>(cells);
	}

	public Cell getCellAt(int index) {
		return cells.get(index);
	}

	public int getCellCount() {
		return this.cells.size();
	}

	@Override
	public boolean equals(Object arg0) {
		if (arg0 == null) {
			return false;
		}
		if (!(arg0 instanceof Row)) {
			return false;
		}
		final Row obj = (Row) arg0;
		if (this.getCells() == null ^ obj.getCells() == null) {
			return false;
		}
		if (this.getCells() != null && obj.getCells() != null) {
			if (this.getCellCount() != obj.getCellCount())
				return false;
			Cell[] thisArr = this.getCells().toArray(
					new Cell[this.getCellCount()]);
			Cell[] objArr = obj.getCells()
					.toArray(new Cell[obj.getCellCount()]);
			for (int i = 0; i < this.getCellCount(); i++)
				if (!thisArr[i].equals(objArr[i]))
					return false;
		}
		return this.index == obj.getIndex();
	}

	@Override
	public int hashCode() {
		if (fHashCode == 0) {
			fHashCode = 7 + 11 * this.index ^ 23;
			if (this.cells != null)
				fHashCode ^= 29 * this.cells.hashCode();
		}
		return fHashCode;
	}

	@Override
	public Iterator<Cell> iterator() {
		return cells.iterator();
	}

}
